
package com.idealighter.game.rank.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.constant.SysConfigId;
import com.idealighter.game.dictionary.dic.SysconfigDic;
import com.idealighter.game.dictionary.domain.SysconfigDomain;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.player.service.IPlayerService;
import com.idealighter.game.player.service.bo.PlayerRankBo;
import com.idealighter.game.player.struct.Player;
import com.idealighter.game.schedule.manager.ScheduleMgr;
import com.idealighter.utils.check.EmptyUtil;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;

@Singleton
@Slf4j
public class RankMgr {

  private static final int RANK_LIMIT = 50;

  private static final long RANK_MAX_RANK = 21 * 1000 * 1000 * 10 * 10;


  private SysconfigDic sysconfigDic;

  private RankMsgMgr rankMsgMgr;

  private IPlayerService playerService;

  private List<PlayerRankBo> goldOne;

  private List<PlayerRankBo> goldTwo;

  private List<PlayerRankBo> winGoldOne;

  private List<PlayerRankBo> winGoldTwo;

  private volatile int activeIndex = 0;

  /**
   * 构造函数.
   * 
   * @param scheduleMgr .
   * @param playerService .
   * @param rankMsgMgr .
   * @param sysconfigDic .
   */
  @Inject
  public RankMgr(ScheduleMgr scheduleMgr, IPlayerService playerService, RankMsgMgr rankMsgMgr,
      SysconfigDic sysconfigDic) {
    this.playerService = playerService;
    this.rankMsgMgr = rankMsgMgr;
    this.sysconfigDic = sysconfigDic;
    log.info("启动排行榜刷新，每{}分钟刷新一次", 30);
    scheduleMgr.scheduleWithFixedDelay(this::refreshRankList, 0, 30, TimeUnit.MINUTES, null);
  }

  /**
   * 刷新排行榜列表.
   */
  public void refreshRankList() {
    log.info("开始刷新排行榜");
    try {
      int rankLimit = getRankLimit();
      int limit = 0;
      List<PlayerRankBo> playerRankDomains =
          playerService.selectBeyoudRankList(RANK_MAX_RANK, rankLimit);
      if (EmptyUtil.listIsNotEmpty(playerRankDomains)) {
        limit = playerRankDomains.size();
        Collections.shuffle(playerRankDomains);
      }
      if (limit < rankLimit) {
        limit = rankLimit - limit;
        List<PlayerRankBo> temp = playerService.selectBelowRankList(RANK_MAX_RANK, rankLimit);
        if (EmptyUtil.listIsNotEmpty(temp)) {
          playerRankDomains.addAll(temp);
        }

      }
      if (EmptyUtil.listIsEmpty(playerRankDomains)) {
        log.error("排行榜没有数据，没有用户?");
      }

      log.info("金币排行榜 加载{}", playerRankDomains.size());

      List<PlayerRankBo> winGolds = playerService.selectTopWinGold(rankLimit);

      log.info("赢分排行榜 加载{}", winGolds.size());

      if (activeIndex == 1) {
        goldTwo = playerRankDomains;
        winGoldTwo = winGolds;
        activeIndex = 2;
      } else {
        goldOne = playerRankDomains;
        winGoldOne = winGolds;
        activeIndex = 1;
      }
    } catch (Exception exception) {
      log.error("加载排行榜出现异常", exception);
    }
  }


  /**
   * 获取排行榜限制.
   * 
   * @return 条数.
   */
  private int getRankLimit() {
    SysconfigDomain cfgDom = sysconfigDic.get(SysConfigId.RANK_LIMIT);
    return cfgDom == null ? RANK_LIMIT : Integer.parseInt(cfgDom.getVal());
  }



  /**
   * 排行榜列表.
   * 
   * @param player 玩家.
   * @return 金币排行榜.
   */
  public ResMessage rankGoldList(Player player) {
    List<PlayerRankBo> playerRankDomains = null;
    if (activeIndex == 1) {
      playerRankDomains = goldOne;
    } else {
      playerRankDomains = goldTwo;
    }
    return rankMsgMgr.resGoldRankList(playerRankDomains);
  }

  /**
   * 赢分榜.
   * 
   * @param player 玩家.
   * @return 赢分榜列表.
   */
  public ResMessage winGoldRankList(Player player) {
    List<PlayerRankBo> playerRankDomains = null;
    if (activeIndex == 1) {
      playerRankDomains = winGoldOne;
    } else {
      playerRankDomains = winGoldTwo;
    }
    return rankMsgMgr.resWinGoldRankList(playerRankDomains);
  }

}
